<?php
/**
 * Created by PhpStorm.
 * User: GAX
 * Date: 2018-07-31 15:02
 * 网络部-程序小组.
 */

namespace plugins\multiupload\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Request;
use think\Session;

class AdminIndexController extends PluginAdminBaseController
{
    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id(); //获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign('admin_id', $adminId);
        } else {
            $this->error('未登录');
        }
    }

    public function index()
    {
        $userName = Session::get('name');

        $categories = Db::name('portal_category')->where('list_tpl', 'product_list')->select();

        $this->assign('name', $userName);
        $this->assign('categories', $categories);

        return $this->fetch('/admin_index');
    }

    public function downloadtemplate(Request $request)
    {
        $host = $request->host();

        dump($host);
        $folder = __DIR__ . "/../";

        $written_file = $folder . "template.xls";

        copy($written_file, __DIR__ . "/../../../template.xls");

        header("Location: http://" . $host . "/template.xls");

        die;
    }

    public function upload()
    {

        $file = request()->file('file');

        $fileinfo = $file->getInfo();

        $name = $fileinfo['name'];

        $ext = explode(".", $name);

        $type = $ext[1];

        $filename = $fileinfo['tmp_name'];

        $type = $type == 'xls' ? 'Excel5' : 'Excel2007';

        ini_set('max_execution_time', '0');

        // 判断使用哪种格式
        $objReader = \PHPExcel_IOFactory::createReader($type);
        $objPHPExcel = $objReader->load($filename);
        $sheet = $objPHPExcel->getSheet(0);
        // 取得总行数
        //$highestRow = $sheet->getHighestRow();
        // 取得总列数
        //$highestColumn = $sheet->getHighestColumn();
        //循环读取excel文件,读取一条,插入一条

        $allColumn = $sheet->getHighestColumn(); //**取得最大的列号*/
        $allRow = $sheet->getHighestRow(); //**取得一共有多少行*/
        $ColumnNum = \PHPExcel_Cell::columnIndexFromString($allColumn); // 列号 转 列数

        $data = array();
        for ($rowIndex = 1; $rowIndex <= $allRow; $rowIndex++) { //循环读取每个单元格的内容。注意行从1开始，列从A开始
            for ($colIndex = 0; $colIndex <= $ColumnNum; $colIndex++) {
                $data[$rowIndex][] = (string) $sheet->getCellByColumnAndRow($colIndex, $rowIndex)->getValue();
            }
        }

        #dump($data);
        $category = request()->post('category');
        foreach ($data as $k => $v) {
            if ($k !== 1) {
                $post = Db::name('portal_post')->where('post_title', $v[0])->find();

                if (empty($post)) {
                    $path = $v[1];
                    $info = explode("/", $path);
                    $name = isset($info[1]) ? $info[1] : '';
                    $more = '{"audio":"","video":"","thumbnail":"","template":"product_article","photos":[{"url":"' . $v[1] . '","name":"' . $name . '"}]}';
                    $portal_post['post_title'] = $v[0];
                    $portal_post['post_status'] = 1;
                    $portal_post['user_id'] = 1;
                    $portal_post['more'] = $more;
                    $portal_post['create_time'] = time();
                    $portal_post['update_time'] = time();
                    $portal_post['published_time'] = time();

                    $insert_id = Db::name('portal_post')->insertGetId($portal_post);
                    $category_data['post_id'] = $insert_id;
                    $category_data['category_id'] = $category;
                    $category_data['status'] = 1;
                    Db::name('portal_category_post')->insert($category_data);
                }

            }

        }

        echo "<script>alert('导入成功');history.go(-1);</script>";

        die;

    }

}
